package com.deyuanyun.pic.ae.controller;

import com.deyuanyun.pic.ae.controller.dto.*;
import com.deyuanyun.pic.ae.controller.vo.PressObjectVO;
import com.deyuanyun.pic.ae.controller.vo.SewerAuditVO;
import com.deyuanyun.pic.ae.controller.vo.SewerVO;
import com.deyuanyun.pic.ae.domain.Sewer;
import com.deyuanyun.pic.ae.enumeration.AEExportEnum;
import com.deyuanyun.pic.ae.enumeration.FindTypeEnum;
import com.deyuanyun.pic.ae.service.SewerService;
import com.deyuanyun.pic.common.dto.PageJsonBean;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.common.util.poi.ExcelUtil;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.settings.util.DownExcelUtil;
import com.github.pagehelper.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * “地下排水排污设施” 管理控制类
 *
 * 方法详情：<br/>
 *
 * @author huanglw
 * @date  2016-11-11
 *
 * History: [修改人][时间][修改描述]
 */
@Controller
@RequestMapping("/ae/sewer")
public class SewerController extends AEBaseController {

    @Resource
    private SewerService sewerService;

    /**
     * 跳转到 “地下排水排污设施” 管理页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/index.html")
    public ModelAndView index(ModelMap model){
        return forword("/ae/sewer", model);
    }

    /**
     * 条件 查询“地下排水排污设施”
     *
     * @param sewerQuery
     */
    @RequestMapping("/query.json")
    @ResponseBody
    public void query(SewerQuery sewerQuery) {
        PageJsonBean jsonBean = new PageJsonBean();
        jsonBean.setList(this.queryVO(sewerQuery));
        AjaxSupport.sendSuccessText("success", jsonBean);
    }

    /**
     * 条件 导出“地下排水排污设施”
     *
     * @param sewerQuery
     */
    @RequestMapping("/export.json")
    @ResponseBody
    public void export(SewerQuery sewerQuery) throws IOException {
        List list = this.queryVO(sewerQuery);
        AEExportEnum exportEnum = AEExportEnum.Sewer;
        String[] columnKeys = {"pipeLineName", "name", "type", "owner", "telephone", "relativePositionRelation",
                "beginNagmInput", "beginNagmMileageInput", "endNagmInput", "endNagmMileageInput", "minDistance", "crossNagmInput",
                "crossNagmMileageInput", "locationDescription", "confinedSpace", "recorder", "recordTime", "remarks"};
        DownExcelUtil.export(exportEnum.getFileName(), aeTemplatePath+exportEnum.getTempPath(), exportEnum.getBeginRow(), list, columnKeys);
    }

    /**
     * 根据id查询 “地下排水排污设施”
     *
     * @param id
     */
    @RequestMapping("/find.json")
    @ResponseBody
    public void find(String id, FindTypeEnum type) throws IOException {

        Asserts.assertLogic(StringUtils.isNullEmpty(id), "地下排水排污设施id 不能为空");
        Sewer sewer = sewerService.find(id);
        if (FindTypeEnum.edit.equals(type)) {
            SewerEdit sewerEdit = new SewerEdit();
            sewerEdit.fromDomain(sewer);
            AjaxSupport.sendSuccessText("", sewerEdit);
        }
        if (FindTypeEnum.show.equals(type)) {
            SewerAuditVO sewerVO = new SewerAuditVO();
            sewerVO.fromDomain(sewer);
            AjaxSupport.sendSuccessText("", sewerVO);
        }
        AjaxSupport.sendSuccessText("", "类型错误，没有任何数据");
    }

    /**
     * 创建 或者 编辑“地下排水排污设施”
     *
     * @param sewerEdit
     */
    @RequestMapping("/edit.json")
    @ResponseBody
    public void edit(SewerEdit sewerEdit) {
        int updateRows = sewerService.edit(sewerEdit.toDomain(), sewerEdit.getImgs());
        AjaxSupport.sendSuccessText("success", updateRows);
    }

    /**
     * 审核“地下排水排污设施”
     *
     * @param aeBaseAudit
     */
    @RequestMapping("/audit.json")
    @ResponseBody
    public void audit(AEBaseAudit aeBaseAudit) {
        Asserts.assertLogic(StringUtils.isNullEmpty(aeBaseAudit.getId()), "地下排水排污设施id 不能为空");
        Asserts.assertLogic(StringUtils.isNullEmpty(aeBaseAudit.getDataStated()), "审核状态 不能为空");
        int updateRows = sewerService.audit(aeBaseAudit.toSewer());
        AjaxSupport.sendSuccessText("success", updateRows);
    }

    /**
     * 删除“地下排水排污设施”
     *
     * @param id
     */
    @RequestMapping("/delete.json")
    @ResponseBody
    public void delete(String id) {
        int updateRows = sewerService.delete(id);
        AjaxSupport.sendSuccessText("success", updateRows);
    }

    private Page<SewerVO> queryVO(SewerQuery sewerQuery) {
        return new SewerVO().fromDomainList(sewerService.query(sewerQuery));
    }
}
